<template>
    <div class="topbar">
        <ul>
            <li v-for="(item, i) in navs" :key="i" :class="index == i ? 'active' : ''" @click="tabFn(i)">
                <span>{{ item.name }}</span>
                <el-icon @click.stop="removeFn(i)">
                    <Close />
                </el-icon>
            </li>
        </ul>
        <div @click="emptyFn">关闭全部</div>
    </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia';
import { userTabStore } from '../stores/taBar'
import { useRouter } from 'vue-router'
const $router = useRouter();
const tabStore = userTabStore()
let { navs, index } = storeToRefs(tabStore)
const tabFn = (num: number) => {
    tabStore.changeIndex(num);
    if (navs.value && navs.value[num]) {
        $router.push({ path: navs.value[num].path })
    }
}
const removeFn = (index: number) => {
    tabStore.remove(index)
}
const emptyFn = () => {
    tabStore.empty();
    $router.push({ path: '/' })
}
</script>

<style lang="scss">
@import '../common/scss/variable.scss';

.topbar {
    background-color: #fff;
    width: 100%;
    height: 41px;
    border-bottom: 1px solid #f1f1f1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;

    div {
        width: 80px;
        font-size: 15px;
        color: #ccc;
        cursor: pointer;
    }

    ul {
        width: 100%;
        display: flex;

        li {
            height: 40px;
            border-right: 1px solid #f0f0f0;
            padding: 0 20px;
            line-height: 40px;
            font-size: 15px;
            color: #666;
            border-radius: 5px;
            position: relative;

            .el-icon {
                position: absolute;
                top: 50%;
                right: 10px;
                transform: translateY(-50%);
                cursor: pointer;
            }

            span {
                margin-right: 15px;
            }

            &.active {
                background-color: $bgColor;
                color: #fff;
            }
        }
    }
}
</style>